<?php
/**
 * 客户应付款统计
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Model
 * @author    	zmx
 * @version 	2.1,2013-03-25
 */
class ClientStatModel extends ClientStatPublicModel{
	/**
	 * 取合计数据
	 *厂家 229  物流 329 
	 * @return array
	 */ 
	public function getIndex() { 
		if (!empty($_POST['date']['from_paid_date']) && !empty($_POST['date']['to_paid_date'])){ 
			$money = '(should_paid)*income_type*-1'; 
			$having = $_POST['show']==1 ? '' : ' having(sum('.$money.')!=0)';	
			$s_where = 'object_type!=129';	
		}else{
			if ($_POST['date']['lt_paid_date']) {
				$money = '(original_money+discount_money)*income_type*-1';
				$s_where = 'object_type!=129';
			}else {
				$money = 'need_paid*income_type*-1';
				///显示所有 
				$s_where = $_POST['show']==1 ? '( is_close=0 or (  is_close>0 and object_type= 103 and object_type_extend=3 ) )' : 'is_close=0 and need_paid!=0'; 
			}
			/// 是否显示0款项客户
			$having = $_POST['show']==1 ? '' : 'having(sum('.$money.')!=0)';		
		}
                if(intval($_GET['group'])==0){
                    $group  = 'country_id ';
                    $id     = ',country_id as id';
                }elseif(intval($_GET['group'])==1){
                    $group  = 'country_id,city_id ';
                    $id     = ',concat(city_id,"_",country_id) as id';
                }else{
                    $group  = 'comp_id';
                }
 
		$where = _search($s_where.' and '._search_array(_getSpecialUrl($_GET)));  
		if(isset($_POST['query']['country_id']) && $_POST['query']['country_id']==0 && intval($_GET['group'])==1){
                    $where .= ' and (country_id=0 or country_id is null)';
                }
                if(isset($_POST['query']['city_id']) && $_POST['query']['city_id']==0 && intval($_GET['group'])>1){
                    $where .= ' and (city_id=0 or city_id is null)';
                    (intval($_POST['query']['country_id'])==0) && $where .= ' and country_id=0';
                    
                }
                
		/// 取明细数组
		$sql_field 		= 'select a.*,sum('.$money.') as total_need_paid '.$this->fundsField().', b.email,b.mobile,b.contact,GROUP_CONCAT(paid_id) as group_paid_id,country_id,city_id '.$id.'
						from client_paid_detail as a inner join company as b on a.comp_id=b.id and b.comp_type=1 left join district c on c.id=country_id
						where '.$where.' ';
		$sql_group 		 =	' 	group by '.$group.',currency_id '.$having.'
								order by district_name,country_id desc,city_id,comp_no desc,comp_id,currency_id,basic_id ';
		$sql_group_total =	' group by  '.$group.',currency_id '.$having.'
							  order by currency_id ';
		$sql			=	$sql_field.$sql_group; 
                if(intval($_GET['group'])>0){   //展开不分页
                    $detail_data	= M()->query($sql);  
                }else{
                    $detail_data	= _listStat($sql);  
                }
		foreach ((array)$detail_data as $key => $value) { 
			$paid_id_array[]	=	intval($value['group_paid_id']); 
		} 
		if (is_array($paid_id_array)){
			/// 取指定客户合计数组
			$sql = 'select client_paid_detail.currency_id,comp_id,sum('.$money.') as total_comp_need_paid
					from client_paid_detail inner join company on company.id=client_paid_detail.comp_id 
					where paid_id in ('.join(',',$paid_id_array).')
					group by '.$group.',client_paid_detail.currency_id  '.$having.'
					order by comp_id,client_paid_detail.currency_id';
			$main_data = $this->db->query($sql);
		}
		
		foreach ((array)$main_data as $key => $value) {
			$main_data_format[$value['comp_id'].'_'.$value['currency_id']]['total_comp_need_paid']	=	$value['total_comp_need_paid'];
		} 
		$list = array();
		
		if (!empty($_POST['date']['from_paid_date']) && !empty($_POST['date']['to_paid_date'])){
			$url_date = '/from_paid_date/'.formatDate($_POST['date']['from_paid_date']).'/to_paid_date/'.formatDate($_POST['date']['to_paid_date']);
			$sourcetype = 4;
		}else{
			if(empty($_POST['date']['lt_paid_date'])){
				$url_date = '';
				$sourcetype = 1;///3
			}else {
				$url_date = '/to_paid_date/'.formatDate($_POST['date']['lt_paid_date']);
				$sourcetype = 4;
			}			
		}  
		foreach ($detail_data as $key => $value) {
			$value['detail_url'] 		= U('/ClientStat/view/type/'.$sourcetype.'/client_id/'.$value['comp_id'].'/currency_id/'.$value['currency_id'].'/basic_id/'.$value['basic_id'].$url_date);
			$value['total_comp_need_paid']	= $main_data_format[$value['comp_id'].'_'.$value['currency_id']]['total_comp_need_paid'];
			$list[] = $value;
		}  
		$list 	= $this->addRemindMoney(_formatList($list));   
		$list	= $this->getTotalCurrencyInfo(&$list,array('total_need_paid'));    
		///没有分页的合计
		///合计为分页的总数量
		$sql_total		=	$sql_field.$sql_group_total;
		$detail_total 	= _formatList($this->db->query($sql_total));
		$detail_total	= $this->getTotalCurrencyInfo(&$detail_total,array('total_need_paid'));     
		$list['page_total']	= $detail_total;
                foreach($list['list'] as $key=>$val){
                    if($val['country_id']==0){
                        $list['list'][$key]['country_name']=L('else');
                    }
                    if($val['city_id']==0){
                        $list['list'][$key]['city_name']=L('else');
                    }
                }
//		echo '<pre>';print_r($list);exit;
		return $list;
	}
}